import pandas as pd


def generate_travel_advice(max_temp, total_rainfall, wind_speed, humidity_diff):
    """根据天气参数生成出行建议"""
    advice = []

    # 温度相关建议
    if max_temp >= 35:
        advice.append("气温极高，建议减少户外活动，注意防暑降温，多喝水")
    elif max_temp >= 30:
        advice.append("气温较高，外出请戴帽子、防晒霜，适当补充水分")
    elif max_temp <= 5:
        advice.append("天气寒冷，外出请穿着保暖衣物，注意防寒保暖")
    elif max_temp <= 0:
        advice.append("天气严寒，建议减少户外活动，外出需做好全面保暖措施")

    # 降雨相关建议
    if total_rainfall > 70:
        advice.append("特大暴雨，建议避免外出，注意防洪防灾")
    elif total_rainfall >= 30:
        advice.append("暴雨天气，建议减少不必要出行，出行请带雨具并注意安全")
    elif total_rainfall >= 15:
        advice.append("大雨天气，出行请带雨具，驾车请注意路面积水")
    elif total_rainfall >= 5:
        advice.append("中雨天气，建议携带雨具")
    elif total_rainfall > 0:
        advice.append("有小雨，建议带上雨具")

    # 风速相关建议
    if wind_speed >= 30:
        advice.append("强风天气，注意户外安全，减少户外活动")
    elif wind_speed >= 20:
        advice.append("大风天气，户外活动需注意安全，尤其注意高空坠物")

    # 湿度相关建议
    if humidity_diff < 2:
        advice.append("空气十分潮湿，注意保持衣物干燥")
    elif humidity_diff > 10:
        advice.append("空气干燥，注意补充水分")

    # 组合建议
    if not advice:
        return "天气适宜，适合各类户外活动"
    else:
        return "；".join(advice) + "。"


def generate_weather_description(max_temp, total_rainfall, mean_pressure, wind_speed, dew_point, date):
    # 温度描述
    if max_temp >= 35:
        temp_desc = "酷热"
    elif max_temp >= 30:
        temp_desc = "炎热"
    elif max_temp >= 25:
        temp_desc = "温暖"
    elif max_temp >= 20:
        temp_desc = "舒适"
    elif max_temp >= 15:
        temp_desc = "凉爽"
    elif max_temp >= 10:
        temp_desc = "寒冷"
    else:
        temp_desc = "严寒"

    # 降雨描述
    if total_rainfall == 0:
        rain_desc = "无降雨"
    elif total_rainfall < 5:
        rain_desc = "小雨"
    elif total_rainfall < 15:
        rain_desc = "中雨"
    elif total_rainfall < 30:
        rain_desc = "大雨"
    elif total_rainfall < 70:
        rain_desc = "暴雨"
    else:
        rain_desc = "特大暴雨"

    # 风力描述
    if wind_speed < 10:
        wind_desc = "微风"
    elif wind_speed < 20:
        wind_desc = "小风"
    elif wind_speed < 30:
        wind_desc = "大风"
    else:
        wind_desc = "强风"

    # 湿度描述（基于露点温度与最高温度的差值）
    humidity_diff = max_temp - dew_point
    if humidity_diff < 2:
        humidity_desc = "十分潮湿"
    elif humidity_diff < 5:
        humidity_desc = "潮湿"
    elif humidity_diff < 10:
        humidity_desc = "较为干燥"
    else:
        humidity_desc = "干燥"

    # 气压描述
    if mean_pressure < 1000:
        pressure_desc = "低气压"
    elif mean_pressure < 1010:
        pressure_desc = "稍低气压"
    elif mean_pressure < 1020:
        pressure_desc = "正常气压"
    else:
        pressure_desc = "高气压"

    # 生成出行建议
    travel_advice = generate_travel_advice(max_temp, total_rainfall, wind_speed, humidity_diff)

    # 生成综合天气描述
    if total_rainfall > 0:
        weather_desc = f"{date}天气：{temp_desc}，{rain_desc}，{wind_desc}，{humidity_desc}，{pressure_desc}系统。\n出行建议：{travel_advice}"
    else:
        weather_desc = f"{date}天气：{temp_desc}，晴朗，{wind_desc}，{humidity_desc}，{pressure_desc}系统。\n出行建议：{travel_advice}"

    return weather_desc


def process_weather_data(csv_file):
    # 读取CSV文件
    df = pd.read_csv(csv_file)

    # 修正列名拼写错误
    column_mapping = {
        'Total Rainfal': 'Total Rainfall',
        'ean Dew Point Temperature': 'Mean Dew Point Temperature'
    }

    df = df.rename(columns=column_mapping)

    # 生成天气描述
    weather_descriptions = []
    for _, row in df.iterrows():
        desc = generate_weather_description(
            row['Maximum Temperature'],
            row['Total Rainfall'],
            row['Mean Pressure'],
            row['Mean Wind Speed'],
            row['Mean Dew Point Temperature'],
            row['Date']
        )
        weather_descriptions.append(desc)

    # 添加描述到数据框
    df['Weather Description'] = weather_descriptions

    return df


if __name__ == "__main__":
    weather_df = process_weather_data("常州.csv")
